"""能源市场可视化工具

提供能源市场模拟结果的可视化功能，包括：
- 供需曲线图
- 发电燃料类型分布图

依赖:
    matplotlib (可选): 用于绘图功能

示例:
    >>> from energy_market import EnergyMarket
    >>> market = EnergyMarket()
    >>> # ...添加参与者...
    >>> price, qty, supply, demand = market.run_auction()
    >>> plot_supply_demand_curve(supply, demand, price, qty)
    >>> plot_participant_distribution(market.participants)
"""

import matplotlib.pyplot as plt
from typing import List, Tuple

def plot_supply_demand_curve(supply_curve: List[Tuple[float, float]], 
                           demand_curve: List[Tuple[float, float]],
                           clearing_price: float,
                           clearing_quantity: float):
    """绘制能源市场供需曲线图
    
    参数:
        supply_curve (List[Tuple[float, float]]): 供给曲线数据 [(价格, 数量)] 按价格升序排列
        demand_curve (List[Tuple[float, float]]): 需求曲线数据 [(价格, 数量)] 按价格降序排列
        clearing_price (float): 市场出清价格(元/kWh)
        clearing_quantity (float): 市场出清电量(kWh)
    
    返回:
        None: 直接显示matplotlib图形
        
    可视化效果:
        - 蓝色实线: 供给曲线
        - 红色实线: 需求曲线
        - 绿色圆点: 市场均衡点
        - 灰色虚线: 均衡价格和数量参考线
        
    示例:
        >>> supply = [(0.2, 100), (0.3, 300), (0.4, 500)]
        >>> demand = [(0.5, 400), (0.4, 300), (0.3, 200)]
        >>> plot_supply_demand_curve(supply, demand, 0.35, 350)
    """
    plt.figure(figsize=(10, 6))
    
    # 提取价格和数量
    s_prices, s_quantities = zip(*supply_curve)
    d_prices, d_quantities = zip(*demand_curve)
    
    # 绘制曲线
    plt.plot(s_quantities, s_prices, 'b-', label='Supply Curve')
    plt.plot(d_quantities, d_prices, 'r-', label='Demand Curve')
    
    # 标记均衡点
    plt.axvline(x=clearing_quantity, color='gray', linestyle='--')
    plt.axhline(y=clearing_price, color='gray', linestyle='--')
    plt.plot(clearing_quantity, clearing_price, 'go', label='Equilibrium')
    
    # 添加标签和标题
    plt.xlabel('Quantity (kWh)')
    plt.ylabel('Price (¥/kWh)')
    plt.title('Energy Market Supply-Demand Curve')
    plt.legend()
    plt.grid(True)
    
    plt.show()

def plot_participant_distribution(participants: list):
    """绘制能源市场参与者分布图
    
    参数:
        participants (list): 市场参与者列表，包含Generator和Consumer对象
    
    返回:
        None: 直接显示matplotlib图形
        
    可视化效果:
        - 饼图显示发电燃料类型分布
        - 自动计算各类别占比
        - 仅显示发电侧参与者(Generator对象)
        
    示例:
        >>> market = EnergyMarket()
        >>> market.add_participant(Generator("风电场", 0.25, 300, "wind"))
        >>> market.add_participant(Generator("火电厂", 0.35, 500, "coal"))
        >>> market.add_participant(Consumer("居民", 0.30, 200))
        >>> plot_participant_distribution(market.participants)
    """
    # 按类型分组
    generators = [p for p in participants if hasattr(p, 'fuel_type')]
    consumers = [p for p in participants if not hasattr(p, 'fuel_type')]
    
    # 按燃料类型分组
    fuel_types = {}
    for gen in generators:
        fuel_types[gen.fuel_type] = fuel_types.get(gen.fuel_type, 0) + gen.capacity
    
    # 准备数据
    labels = list(fuel_types.keys())
    sizes = list(fuel_types.values())
    
    # 绘制饼图
    plt.figure(figsize=(8, 8))
    plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
    plt.title('Generation Capacity by Fuel Type')
    plt.show()